package gstudio.mp3;

import gstudio.mp3.DoMp3CutFile;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;

public class Mp3CutWithSpinnerComposite extends Composite {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("Text Table Editor");
		shell.setLayout(new FillLayout());
		Mp3CutWithSpinnerComposite msc = new Mp3CutWithSpinnerComposite(shell,
				SWT.NONE);
		msc.setInput(100, 200, "inputMp3Path", "outptMp3Path");
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	private Composite mComp;
	private Spinner spinnerStart, spinnerEnd;
	private Button cutButton;
	private MiniPlayerComposite player;
	// private Label outptMp3PathLabel;
	private String inputMp3Path, outputMp3Path;

	public Mp3CutWithSpinnerComposite(Composite parent, int style) {
		super(parent, style);
		initGUI();
	}

	private void initGUI() {
		this.setLayout(new FillLayout());

		mComp = new Composite(this, SWT.BORDER);
		{
			GridLayout gl = new GridLayout(5, false);
			gl.marginHeight = -10;
			gl.marginWidth = 0;
			mComp.setLayout(gl);

			GridData gd = new GridData(GridData.FILL_HORIZONTAL);
			mComp.setLayoutData(gd);
		}

		spinnerStart = new Spinner(mComp, SWT.NONE);
		spinnerStart.setMaximum(Integer.MAX_VALUE);
		spinnerStart.setIncrement(5000);
		{
			GridData gd = new GridData();
			gd.widthHint = 40;
			gd.horizontalIndent = 0;
			gd.verticalIndent = 0;
			spinnerStart.setLayoutData(gd);

		}
		spinnerStart.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event arg0) {
				cutButton.setEnabled(true);
			}

		});
		new Label(mComp, SWT.NONE).setText("  ");

		spinnerEnd = new Spinner(mComp, SWT.NONE);
		spinnerEnd.setIncrement(5000);
		spinnerEnd.setMaximum(Integer.MAX_VALUE);
		spinnerEnd.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event arg0) {
				cutButton.setEnabled(true);
			}

		});
		{
			GridData gd = new GridData();
			gd.widthHint = 40;
			spinnerEnd.setLayoutData(gd);
		}
		// outptMp3PathLabel = new Label(mComp, SWT.BORDER);

		// {
		// GridData gd = new GridData();
		// gd.widthHint = 100;
		//
		// outptMp3PathLabel.setLayoutData(gd);
		// }
		cutButton = new Button(mComp, SWT.NONE);
		cutButton.setText("cut");

		cutButton.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				DoMp3CutFile cut = new DoMp3CutFile();
				int st = spinnerStart.getSelection();
				int ed = spinnerEnd.getSelection();
				System.out.println(st + " " + ed + " " + inputMp3Path + " "
						+ outputMp3Path);
				if (cut.run(st, ed, inputMp3Path, outputMp3Path)) {
					cutButton.setEnabled(false);
				}
			}

		});
		player = new MiniPlayerComposite(mComp, SWT.NONE);

	}

	public void setInput(int start, int end, String inputMp3Path,
			String outputMp3Path) {
		spinnerStart.setSelection(start);
		// outptMp3PathLabel.setText("   " + outputMp3Path + "   ");
		spinnerEnd.setSelection(end);
		cutButton.setEnabled(false);
		this.inputMp3Path = inputMp3Path;
		this.outputMp3Path = outputMp3Path;
		player.setInput(outputMp3Path, false);
		player.run();
	}

	public int[] getStartEnd(){
		return new int[]{spinnerStart.getSelection(),spinnerEnd.getSelection()};
	}
}
